package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpWorkAddDTO;
import org.jsola.hr.dto.EmpWorkUpdateDTO;
import org.jsola.hr.query.EmpWorkQuery;
import org.jsola.hr.service.IEmpWorkService;
import org.jsola.hr.vo.EmpWorkListVO;
import org.jsola.hr.vo.EmpWorkVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrEmpWorkController")
@RequestMapping(HrConstants.API_PATH + "/v1/empWorks")
public class EmpWorkController implements IEmpWorkControllerAPI {

    @Autowired
    private IEmpWorkService empWorkService;

    @Override
    @PostMapping
    public Result<EmpWorkVO> save(@Valid @RequestBody EmpWorkAddDTO empWorkAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工工作经历权限校验

        return Result.success(
                empWorkService.save(empWorkAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpWorkUpdateDTO empWorkUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工工作经历权限校验

        int count = empWorkService.update(empWorkUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{empWorkId}")
    public Result<Integer> delete(@PathVariable String empWorkId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工工作经历权限校验

        int count = empWorkService.deleteByIds(tokenUser, empWorkId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empWorkIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工工作经历权限校验

        int count = empWorkService.deleteByIds(tokenUser, empWorkIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empWorkId}")
    public Result<EmpWorkVO> findById(@PathVariable String empWorkId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工工作经历权限校验

        return Result.success(
            empWorkService.selectById(empWorkId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpWorkListVO>> listAll(@Valid EmpWorkQuery empWorkQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工工作经历权限校验

        return Result.success(
            empWorkService.select(empWorkQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpWorkListVO>> page(@Valid EmpWorkQuery empWorkQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empWorkService.selectPage(empWorkQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @DeleteMapping(value = "/deleteEmpWork/{empWorkId}")
    public Result<Integer> deleteEmpWork(@PathVariable String empWorkId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工培训经历权限校验

        Integer count = empWorkService.deleteEmpWork(tokenUser, empWorkId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


}

